package com.shan.domm.controller;

import com.shan.domm.common.BaseResponse;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.common.ResultUtils;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.model.domain.Dorm;
import com.shan.domm.service.DormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dorm")
@Slf4j
public class DormController {

    @Resource
    private DormService dormService;

    @PostMapping(value = "/add")
    public BaseResponse<Boolean> adddorm(@RequestBody Dorm dorm){
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        Boolean adddorm = dormService.adddorm(dorm);
        return ResultUtils.success(adddorm);
    }

    @PostMapping(value="/delete")
    public BaseResponse<String> deteledorm(@RequestBody Dorm dorm){
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        String deletedorm = dormService.deletedorm(dorm);
        return ResultUtils.success(deletedorm);
    }

    @PostMapping(value = "/update")
    public BaseResponse<String> updatedorm(@RequestBody Dorm dorm){
        if(dorm==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        String updatedorm = dormService.updatedorm(dorm);
        return ResultUtils.success(updatedorm);
    }

    @PostMapping(value = "/search")
    public BaseResponse<Dorm> seaarchdorm(String dormName){
        if(dormName==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        Dorm searchdorm = dormService.searchdorm(dormName);
        return ResultUtils.success(searchdorm);
    }
}
